package com.zhidisoft.admin.mapper;

import java.math.BigInteger;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zhidisoft.admin.entity.Article;
import com.zhidisoft.admin.vo.ArticleVo;

public interface IArticleMapper {
	
	/**
	 * 添加
	 * @param username
	 * @return
	 */
	@Insert("insert into article(id,title,info_title,info,user_id,type_id) values(#{id},#{title},#{infoTitle},#{info},#{userId},#{typeId})")
	public int save(@Param("id") String id
			,@Param("title") String title
			,@Param("infoTitle") String infoTitle
			,@Param("info") String info
			,@Param("userId") String userId
			,@Param("typeId") String typeId);
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Delete("delete from article where id = #{id}")
	public int deleteById(@Param("id") String id);
	
	/**
	 * 修改
	 * @param username
	 * @param id
	 * @return
	 */
	@Update("update article set article = ${articleVo} where id = #{id}")
	public int update(ArticleVo articleVo, @Param("id") String id);
	
	/**
	 * 根据id查询单个
	 * @param id
	 * @return
	 */
	@Select("SELECT * FROM article WHERE id = #{id}")
	public Article findById(@Param("id") String id);
	
	/**
	 * 分页查询
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	@Select("<script>" 
			+ "select a.title,a.create_time createTime,u.username username,t.name typeName from article a "
			+ " inner join user u on u.id=a.user_id" 
			+ " inner join article_type t on t.id=a.type_id" 
			+ " limit #{startIndex},#{pageSize}" + "</script>")
	List<ArticleVo> pageList(
			@Param("startIndex") Integer startIndex,
			@Param("pageSize") Integer pageSize);
	
	/**
	 * 获取总条数
	 * @return
	 */
	@Select("SELECT count(id) FROM article")
	public BigInteger totalCount();
}
